package com.ocm.tourism.core.dao.jpa;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.ocm.tourism.core.dao.ScheduleTourDao;
import com.ocm.tourism.core.domain.SaleOff;
import com.ocm.tourism.core.domain.ScheduleTour;

@Repository("scheduleTourDao")
public class JpaScheduleTourDao extends JpaGenericDao<ScheduleTour> implements ScheduleTourDao {

   public JpaScheduleTourDao() {
      this.setClazz(ScheduleTour.class);
   }

   @SuppressWarnings("unchecked")
   public List<ScheduleTour> findScheduleTours(long agencyId, int firstResult, int maxResults) {
      Query query = this.entityManager().createQuery("SELECT s FROM ScheduleTour s WHERE s.business.id =:agency", ScheduleTour.class).setFirstResult(firstResult).setMaxResults(maxResults);
      query.setParameter("agency", agencyId);
      List<ScheduleTour> agencies = query.getResultList();
      return agencies;
   }

   @Override
   public long countScheduleTours(long agencyId) {
      Query query = this.entityManager().createQuery("SELECT COUNT(s) FROM ScheduleTour s WHERE s.business.id =:agency", Long.class);
      query.setParameter("agency", agencyId);
      long count = (Long) query.getSingleResult();
      return count;
      
   }

   @SuppressWarnings("unchecked")
   public List<SaleOff> findSaleOffs(long tourId, int firstResult, int maxResults) {
      Query query = this.entityManager().createQuery("SELECT s FROM SaleOff s WHERE s.scheduleTour.id =:tour", SaleOff.class).setFirstResult(firstResult).setMaxResults(maxResults);
      query.setParameter("tour", tourId);
      List<SaleOff> saleOffs = query.getResultList();
      return saleOffs;
   }

   @Override
   public long countSaleOffs(long tourId) {
      Query query = this.entityManager().createQuery("SELECT COUNT(s) FROM SaleOff s WHERE s.scheduleTour.id =:tour", Long.class);
      query.setParameter("tour",tourId);
      long count = (Long) query.getSingleResult();
      return count;
   }

   @SuppressWarnings("unchecked")
   public List<SaleOff> findSaleOffAdvertisements(long agencyId, int firstResult, int maxResults){
      Query query = this.entityManager().createQuery("SELECT s FROM SaleOff s left join s.advertisement a WHERE s.scheduleTour.business.id =:agency ORDER BY a.priority, a.modifiedDate DESC", SaleOff.class).setFirstResult(firstResult).setMaxResults(maxResults);
      query.setParameter("agency", agencyId);
      List<SaleOff> saleOffs = query.getResultList();
      return saleOffs;
   }

   @Override
   public long countSaleOffAdvertisements(long agencyId) {
      Query query = this.entityManager().createQuery("SELECT COUNT(s) FROM SaleOff s WHERE s.scheduleTour.business.id =:agency", Long.class);
      query.setParameter("agency", agencyId);
      long count = (Long) query.getSingleResult();
      return count;
   }

   @Override
   public SaleOff findSaleOff(long saleoffId) {
      SaleOff saleOff = this.entityManager.find(SaleOff.class, saleoffId);
      return saleOff;
   }
}
